package test.poi;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

public class Xml2Word {

    /*
     * (non-Javadoc)
     *
     * @see cn.ccb.sarm.bizprocess.assetdeal.exam.approve.bizservice.IWfConferenceBS#writeXML(java.io.OutputStream,
     *      cn.ccb.sarm.bizprocess.assetdeal.exam.approve.common.bo.TaskVO,
     *      java.util.List, cn.ccb.sarm.common.model.bo.WfConferenceBO,
     *      cn.ccb.sarm.common.model.bo.WfConfBpBO)
     */
//    public void writeXML(OutputStream output, TaskVO task, List opinionList, WfConferenceBO conf,
//            WfConfBpBO confBP) throws IOException, IllegalAccessException,
//            InvocationTargetException, NoSuchMethodException {
//        Document doc = DocumentHelper.createDocument();
//        Element root = doc.addElement("conference");
//        Element taskNode = root.addElement("task");
//        addBeanProp(task, taskNode);
//        Element confNode = root.addElement("conferenceInfo");
//        addBeanProp(conf, confNode);
//        Element confBpNode = root.addElement("confBPInfo");
//        addBeanProp(confBP, confBpNode);
//        Element approveNode = root.addElement("approvers");
//
//        addListMapEntry(opinionList, approveNode);
//
//        XMLWriter writer = new XMLWriter(output);
//        writer.write(doc);
//
//    }
//
//    /**
//     * ת��list����Ϊ���Ԫ��
//     *
//     * @param list
//     *            ����list
//     * @param node
//     *            ���ڵ�
//     */
//    private void addListMapEntry(List list, Element node) {
//        Iterator it = list.iterator();
//        while (it.hasNext()) {
//            Map map = (Map) it.next();
//            Element approveNode = node.addElement("approver");
//            addMapEntry(map, approveNode);
//        }
//    }
//
//    /**
//     * ת��beanΪxml
//     *
//     * @param bean
//     *            ����bean
//     * @param node
//     *            ���ڵ�
//     * @throws IllegalAccessException
//     * @throws InvocationTargetException
//     * @throws NoSuchMethodException
//     */
//    private void addBeanProp(Object bean, Element node) throws IllegalAccessException,
//            InvocationTargetException, NoSuchMethodException {
//        Map propMap = BeanUtils.describe(bean);
//        addMapEntry(propMap, node);
//    }
//
//    /**
//     * ת��map����Ϊxml
//     *
//     * @param map
//     *            ��ݶ���
//     * @param node
//     *            ���ڵ�
//     */
//    private void addMapEntry(Map map, Element node) {
//        Set entrySet = map.entrySet();
//        Iterator it = entrySet.iterator();
//        while (it.hasNext()) {
//            Map.Entry e = (Entry) it.next();
//            node.addElement(e.getKey().toString()).addText(
//                    (e.getValue() == null ? "" : e.getValue().toString()));
//        }
//    }
//
//    /*
//     * (non-Javadoc)
//     *
//     * @see cn.ccb.sarm.bizprocess.assetdeal.exam.approve.bizservice.IWfConferenceBS#transformDocument(java.io.OutputStream,
//     *      java.io.FileInputStream, java.io.FileInputStream)
//     */
//    public void transformDocument(OutputStream out, InputStream data, InputStream template)
//            throws TransformerException {
//        TransformerFactory factory = TransformerFactory.newInstance();
//        Transformer transform = factory.newTransformer(new StreamSource(template));
//
//        Result result = new StreamResult(out);
//        transform.transform(new StreamSource(data), result);
//    }


//    public void testWriteXML() throws Exception {
//        IWfConferenceBS conferenceBS = new WfConferenceBS();
//        TaskVO task = new TaskVO();
//        task.setBizTypeName("����ҵ������");
//        List opinionList = new ArrayList();
//        Map m = new HashMap();
//        m.put("userName", "testUser");
//        m.put("userConfRole", "testUserRole");
//        m.put("approverOpinion", "approverOpinion");
//        m.put("lastChangeTime", "time");
//        opinionList.add(m);
//        WfConferenceBO conf = new WfConferenceBO();
//        conf.setConfPlace("place");
//        WfConfBpBO confBP = new WfConfBpBO();
//        confBP.setConfResult("testResult");
//        confBP.setConfOpinion("confOpinion");
//        File tempOutputFile = File.createTempFile("specialApproveTemp", ".xml");
//        OutputStream output = new FileOutputStream(tempOutputFile);
//        conferenceBS.writeXML(output, task, opinionList, conf, confBP);
//        output.flush();
//        output.close();
//
//        OutputStream out = new FileOutputStream(File.createTempFile("specialApproveTemp", ".doc"));
//        File template = ResourceUtils
//                .getFile("classpath:cn/ccb/clpm/wf/common/specialApproveTemplate.xsl");
//        conferenceBS.transformDocument(out, new FileInputStream(tempOutputFile),
//                new FileInputStream(template));
//        output.flush();
//        output.close();
//
//    }

}
